var Util = require("../../utils/utils.js");
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    new: "new",
    hot: "hot",
    imageURL: "../../../../images/a.jfif",
    // 总数据
    list: [],
    // 展示列表
    cardList: [],

    questionType: [],
    sortRule: [{
        text: '默认排序',
        value: 'a'
      },
      {
        text: '难度排序',
        value: 'b'
      },
    ],
    id: 2,
    value2: 'a',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    let {
      id
    } = options;
    this.getQuestionList(id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 前往详情页
  goQuestionShow(data) {
    // console.log(data.currentTarget.dataset);
    // 跳转详情页
    var url = `../questionShow/index?id=${data.currentTarget.dataset.id}`;
    wx.navigateTo({
      url,
      success: function (res) {
        // console.log(res);
        // console.log("跳转成功");
      },
      fail(err) {
        // console.log(err)
      }
    })
  },

  // 触发类型变动
  typeChange(data) {
    // console.log(data);
    let id = data.detail;
    // 更换为类型相关题目
    var cardList = [];
    var list = this.data.list;
    list.map(e => {
      if (e.type == id) {
        cardList.push(e);
      }
    });
    this.setData({
      cardList,
      id
    });
  },

  // 触发排序规则变动
  sortChange({
    detail
  }) {
    // console.log(detail);
    this.sortByRule(detail);
  },

  // 获取题目列表
  getQuestionList(param) {
    const id = param;
    var that = this;
    // 获取全部文章
    var url = `${app.globalData.baseURL}/api/question/question/list`;
    wx.request({
      url,
      success(res) {
        // console.log(res.data.page.list);
        var list = res.data.page.list;
        list = list.map(e => {
          e.createTime = Util.formatTime(new Date(e.createTime));
          e.updateTime = Util.formatTime(new Date(e.updateTime));
          return e;
        })
        var cardList = [];
        list.map(e => {
          if (e.type == id) {
            cardList.push(e);
          }
        });
        that.setData({
          cardList,
          list
        });
        that.sortByRule("a");
      }
    });
    // 获取全部类型
    url = `${app.globalData.baseURL}/api/question/type/list?page=1&limit=100`;
    wx.request({
      url,
      success(res) {
        let {
          list
        } = res.data.page;
        let questionType = [];
        list.map(e => {
          questionType.push({
            text: e.type,
            value: e.id
          });
        });
        that.setData({
          questionType,
          id
        })
      }
    });
  },

  // 排序
  sortByRule(rule) {
    var cardList = this.data.cardList;
    // console.log(cardList);
    if (rule == "a") {
      cardList.sort((a, b) => {
        if (b.displayOrder != a.displayOrder) {
          return b.displayOrder - a.displayOrder;
        } else {
          return a.level - b.level;
        }
      });
    } else if (rule == "b") {
      cardList.sort((a, b) => {
        return b.level - a.level;
      });
    }
    // console.log(cardList);
    this.setData({
      cardList
    });
  }
})